package com.atguigu.yygh.user.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.user.utils.AuthContextHolder;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
//就诊人管理接口
@Slf4j
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {
   //人员就诊信息实体Patient

    @Autowired
    private PatientService patientService;
    //获取就诊人列表

    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request) {
        //因为是登录后的操作，把信息放在了token中通过token获取id等信息
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllUserId(userId);
        return Result.ok(list);
    }
    //添加就诊人ID
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        //获取当前登录的id
        Long userId = AuthContextHolder.getUserId(request);
        log.info("查看是否有userid{}",patient);
        patient.setUserId(userId);
        log.info("查看是否有userid{}",patient);
        patientService.save(patient);
        return Result.ok();

    }


    //根据id获取就诊人信息(不是登录者的信息)
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientId(id);
        return Result.ok(patient);
    }

    //修改就诊人
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return Result.ok();
    }
    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable Long id) {
        patientService.removeById(id);
        return Result.ok();
    }
    //id获取就诊人信息
    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(
            @PathVariable("id") Long id) {
        return patientService.getPatientId(id);
    }
}
